﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlAgilityPack;
using MiniCrawler.DataAccess;
using MiniCrawler.DataAccess.Repositories;
using MiniCrawler.Entity;
using MiniCrawler.UrlFilter;

namespace MiniCrawler.Downloader.ResourceDownloader
{
    public class WallpaperHd : ResourceLinkCollector,IResourceCollector
    {
        private string hostName;
        public WallpaperHd(string hostName, int level) : base(hostName, level)
        {
            this.hostName = hostName;
        }

        public string GetFolderPath(string data)
        {
            // Load data
            var doc = new HtmlDocument();
            doc.LoadHtml(data);

            string xPathExpression = "/html/body/div[@id='body']/div[@id='container']/div[@class='container_right']/div[@class='container_inside']/div[@class='content_background']/div[@id='content']/div[@class='breadCrumbs']";
            var node = doc.DocumentNode.SelectSingleNode(xPathExpression);

            string nodeInnerText = "/";
            if(node != null)
                nodeInnerText =  node.InnerText;

            // Processing text
            string processedText = 
                nodeInnerText
                .Replace("&gt;",">")
                .Replace("&amp;","-")
                .Replace(">", "\\").Replace('&', '-');

            if (!string.IsNullOrEmpty(processedText))
                return processedText;
            else return "/";
        }

        public List<string> GetAllLinks(string data)
        {
            var result = new List<string>();
            string xPathExpression =
                "/html/body/div[@id='body']/div[@id='container']/div[@class='container_right']/div[@class='container_inside']/div[@class='content_background']/div[@id='content']/div[3]/div[@id='wallpaper-resolutions']";
            var doc = new HtmlDocument();
            doc.LoadHtml(data);

            var node = doc.DocumentNode.SelectSingleNode(xPathExpression);
            if (node == null) return result;

            List<string> allLinks = Utils.GetAllLinks(node.InnerHtml);


            string expressionObjectTemplate1 =
                "<Expression><ExpressionParts><Part><Command>StartOfLine</Command><Value></Value></Part><Part><Command>Then</Command><Value>http://wallpaperswide.com/download/</Value></Part><Part><Command>AnythingBut</Command><Value>/</Value></Part><Part><Command>Then</Command><Value>1600x900</Value></Part><Part><Command>Then</Command><Value>.jpg</Value></Part><Part><Command>EndOfLine</Command><Value></Value></Part></ExpressionParts></Expression>";
            string expressionObjectTemplate2 =
                "<Expression><ExpressionParts><Part><Command>StartOfLine</Command><Value></Value></Part><Part><Command>Then</Command><Value>http://wallpaperswide.com/download/</Value></Part><Part><Command>AnythingBut</Command><Value>/</Value></Part><Part><Command>Then</Command><Value>1920x1080</Value></Part><Part><Command>Then</Command><Value>.jpg</Value></Part><Part><Command>EndOfLine</Command><Value></Value></Part></ExpressionParts></Expression>";

            ExpressionObject exp1 = new ExpressionObject(expressionObjectTemplate1);
            ExpressionObject exp2 = new ExpressionObject(expressionObjectTemplate2);

            foreach (var link1 in allLinks)
            {
                var link = Common.FillAbsoluteUrl(hostName, link1);
                if(exp1.IsMatch(link) || exp2.IsMatch(link))
                {
                    result.Add(link);
                }
            }

            return result;
        }
        public List<string> GetResourcePatterns()
        {
            return new List<string>();
        }

        public void GetLinks()
        {
            foreach (var dataLink in Urls)
            {
                string data = Common.GetWebPage(Common.FillAbsoluteUrl(hostName, dataLink.Value));

                string folderPath = GetFolderPath(data);

                var allLinks = GetAllLinks(data);

                if(allLinks != null && allLinks.Count > 0)
                {
                    foreach (var link in allLinks)
                    {
                        var downloadLink = 
                            new DownloadLink
                                {
                                    Path = folderPath,
                                    Value = link,
                                    Website = dataLink.Website,
                                    IsDownloaded = false
                                };
                        DownloadLinkHelper.CreateLink(downloadLink);
                    }
                }

                DataLinkHelper.UpdateLink(dataLink.Value);
            }
        }
    }
}
